package com.securitytrace.dataModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.table.DefaultTableModel;

import com.securitytrace.model.Product;
import com.securitytrace.util.ConvertAdapter;
import com.securitytrace.util.ProductConvertToVector;

public class ProductDataModel extends DefaultTableModel {

	private static final long serialVersionUID = 7067677654300997150L;
	private static final String[] productColumns = new String[] { "类型", "描述" };
	private Map<Integer, Product> productMap = new HashMap<Integer, Product>();
	private int productCurrenPage;
	private int productTotal;
	private ConvertAdapter<Product> productConvertToVector;
	
	@SuppressWarnings("rawtypes")
	private Class[] types = new Class [] {java.lang.String.class, java.lang.String.class};
	private boolean[] canEdit = new boolean [] {false, false};
	
	public ProductDataModel() {
		productConvertToVector = new ProductConvertToVector();
	}
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
        return canEdit [columnIndex];
    }

	@Override
	public Class<?> getColumnClass(int columnIndex) {
        return types[columnIndex];
    }
	
	/** 初始化数据 **/
	public void setDataVector(List<Product> products) {
		for (int row = 0; row < products.size(); row++) {
			productMap.put(row, products.get(row));
		}
		super.setDataVector(productConvertToVector.convertToVector(products),
				productConvertToVector.convertToVector(productColumns));
	}

	/** 获取一行数据 **/
	public Product getRow(int row) {
		return productMap.get(row);
	}

	/*** 更新一行数据 **/
	public void setValueAtRow(Product product, int row) {
		productMap.put(row, product);
		this.setValueAt(product.getName(), row, 0);
		this.setValueAt(product.getOtherInfo(), row, 1);
	}

	/**以下为get/set方法**/
	public Map<Integer, Product> getProductMap() {
		return productMap;
	}
	public void setProductMap(Map<Integer, Product> productMap) {
		this.productMap = productMap;
	}
	public int getProductTotal() {
		if(productTotal > 0) {
			return productTotal;
		}else {
			return 1;
		}
	}
	public void setProductTotal(int productTotal) {
		this.productTotal = productTotal;
	}
	public int getProductCurrenPage() {
		if(0 < productCurrenPage) {
			return productCurrenPage;
		}else {
			return 1;
		}
	}
	public void setProductCurrenPage(int productCurrenPage) {
		this.productCurrenPage = productCurrenPage;
	}
	public String getProductPageInfo() {
		return "共 " + this.getProductTotal() +" 页，当前为第 "+this.getProductCurrenPage()+" 页";
	}
}
